using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Rendering.Universal;

public class FlashLightDetection : MonoBehaviour
{
    public Transform player;
    public List<Collider2D> EnemyList;
    public bool tset;
    public LayerMask layerMask; // 用于指定需要检测的层

    private void Awake()
    {
        EnemyList = new List<Collider2D>();
    }

    public void OnTriggerEnter2D(Collider2D other)
    {
        if (other.CompareTag("Enemy"))
        {
            EnemyList.Add(other);
        }
    }

    public void OnTriggerExit2D(Collider2D other)
    {
        if (other.CompareTag("Enemy"))
        {
            EnemyList.Remove(other);
        }
    }

    public void Update()
    {
        tset = CanSeeEnemies();
    }

    public bool CanSeeEnemies()
    {
        foreach (Collider2D enemy in EnemyList)
        {
            Vector2 directionToEnemy = enemy.transform.position - player.position;
            float distanceToEnemy = directionToEnemy.magnitude;

            // 进行射线检测，使用layerMask忽略指定层
            RaycastHit2D hit = Physics2D.Raycast(player.position, directionToEnemy.normalized, distanceToEnemy, layerMask);

            // 在Scene视图中可视化射线（可选）
            Debug.DrawRay(player.position, directionToEnemy.normalized * distanceToEnemy, Color.red);

            // 确保射线的hit物体不是敌人
            if (hit.collider != null && hit.collider != enemy)
            {
                if (hit.collider.GetComponent<ShadowCaster2D>() != null)
                {
                    return false; 
                }
            }
        }
        return true; 
    }
}